package com.xp.ai.ragdemo;

import com.xp.ai.util.ModelUtils;
import dev.langchain4j.agent.tool.Tool;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.agent.tool.ToolSpecifications;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.Result;
import dev.langchain4j.store.embedding.EmbeddingStore;
import org.simpleframework.xml.stream.Mode;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 *
 * 使用代理实现 整个 rag 流程
 * @author xp
 */
public class RagEasyAiService {

    public static class Tools{
        @Tool("当前时间")
        public String getToday() {
            LocalDateTime date = LocalDateTime.now();
            return date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }
    }




    /***
     * 搭建一个智能客服
     * @param args
     */
    public static void main(String[] args) {
        //1.构建对话大模型
        ChatLanguageModel chatLanguageModel = ModelUtils.getHuoshanv3Model();
        //2.构建对话记忆，将ArrayList 替换为MessageWindowChatMemory
        ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
        //3.构建向量大模型
        EmbeddingModel embeddingModel = ModelUtils.getGjEmbeddingModel();
        //4.构建向量数据库操作类
        EmbeddingStore<TextSegment> embeddingStore = ModelUtils.getPgEmbeddingStore();
        //5.构建向量检索
        ContentRetriever contentRetriever = EmbeddingStoreContentRetriever.builder()
                .embeddingStore(embeddingStore)
                .embeddingModel(embeddingModel)
                .maxResults(3)
                .build();
        //6.构建工具(Tools.class);
        //如果执行本地的增强方法报错了他会自动无效化，真是智能啊
        //构建智能体
        MeituanAiCustomService customService = AiServices.builder(MeituanAiCustomService.class)
                .chatLanguageModel(chatLanguageModel)
                .chatMemory(chatMemory)
                .contentRetriever(contentRetriever)
                .tools(new Tools())
                .build();


        //所有的上面的配置，只是为了下面这个 chat()方法，进行调用
        Result<String> chat = customService.chat("我是小明，我十九岁，这是我的朋友乔治，他十五岁了，我想请问一下入职有什么要求？");
        String content = chat.content();
        System.out.println(content);
        Result<String> chat1 = customService.chat("我的朋友可以顺利入职么？");
        System.out.println(chat1.content());
    }
}
